<?php

namespace app\order\logic;
use app\common\library\RedissLock;
use app\common\service\CommonSystemImportService;
use think\facade\Db;

class OrderDiFourLogic extends BasicLogic
{

    /**
     * @desc    递四方账单对账
     * @author  luckyXiong
     * @date    2024-09-02
     */
    public function import_fang_detail($params){
        try{
            $result = CommonSystemImportService::staticModel()->operate_import_data([
                    $this,//当前对应的service
                    'save',//类型: template 下载模板; save :保存 导入
                    CommonSystemImportService::TYPE_IMPORT_FANG_PAGE,
                    1,
                    $params
                ]
            );
        }catch (\Exception $e) {
            return $this->jsonMessage(201, false, $e->getMessage(), null);
        }
    }



    /**
     * @desc 将导入数据写入目标表
     * @author xx
     * @date 2024-03-21
     */
    public function import_fang_info($params=[]){
        $key                      = 'import_qc_infos:'.time();
        $redis_lock               = new RedissLock();
        $lock                     = $redis_lock::lock($key, $redis_lock::UNBLOCK, 30);
        $waybill_no               = getIsset($params,'waybill_no');
        $total_weight             = getIsset($params,'total_weight');
        $pieces                   = getIsset($params,'pieces');
        $operating_fee            = getIsset($params,'operating_fee');
        $freight                  = getIsset($params,'freight');
        $unit_price               = getIsset($params,'unit_price');
        $createtime               = getIsset($params,'createtime');


        //数据入库逻辑
        $info = [
            'waybill_no'          =>$waybill_no,
            'total_weight'        =>$total_weight,
            'pieces'              =>$pieces,
            'unit_price'          =>$unit_price,
            'operating_fee'       =>$operating_fee,
        ];
        Db::name('checkbill_4px')->insert($info);
        if($waybill_no){
            $create_time = date('Y-m-d H:i:s',strtotime($createtime));
            $date        = '2024-12';
            //同时写入运费总表中
            $inser_data  =
                [
                    'transfer_number'              => $waybill_no,
                    'freight'                      => $freight,
                    'tracking_company_number'      => 2,
                    'date'                         => $date,
                    'createtime'                   => $create_time,
                    'weight'                       => $total_weight*1000,
                ];
            Db::name('ordercheck_logistics')->insert($inser_data);

            //同时写入订单表中;
            if($freight>0){
                $update_order_data =
                    [
                        'freight'                      => $freight,
                        'freight_weight'               => $total_weight*1000,
                    ];
                Db::name('order_achievement')->where('tracking_number',$waybill_no)->update($update_order_data);
            }
        }
        $lock->unlock();
        $info['status'] = 1;
        return  $info;
    }


    /**
     * @param $row
     * @return string[]
     * @desc 数据title 导入组装数据结构
     */
    public function get_excel_row_data($row){
        return [
            'waybill_no'    => empty($row['E'])? '': trim($row['E']),
            'total_weight'  => empty($row['S'])? '': trim($row['S']),
            'pieces'        => empty($row['X'])? '': trim($row['X']),
            'operating_fee' => empty($row['AG'])? '': trim($row['AG']),
            'freight'       => empty($row['AC'])? '': trim($row['AC']),
            'unit_price'    => empty($row['AF'])? '': trim($row['AF']),
            'createtime'    => empty($row['H'])? '': trim($row['H']),
        ];
    }

}